//
//  RGMyworkVediosViewModel.m
//  RedGrass
//
//  Created by wukong on 2019/2/18.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGMyworkVediosViewModel.h"
#import "HttpRequest.h"

@interface RGMyworkVediosViewModel()

@property (weak, nonatomic)   id<RGMainPageViewProtocol> protocol;

@end

@implementation RGMyworkVediosViewModel

-(instancetype) initWithProtocol:(id<RGMainPageViewProtocol>)protocol{
    if(self = [super init]){
        self.protocol = protocol;
        self.pagaNum = 1;
        self.mainCommentPageNum = 1;
        self.total = 0;
        self.currentVedioId = 0;
        self.currentVedioIndex = 0;
        self.VedioPageNum = 1;
    }
    return self;
}



-(void)loadVediosWithIsLoadMore:(BOOL)isLoadMore isLoadWorkVedio:(BOOL)isloadWorkVedio{
    NSDictionary *dic;
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSInteger userID = [RGUserInfoModel getUserInfo].ID;
    if(isLoadMore){
        self.VedioPageNum ++;
        dic = @{@"token":SafeStr(token),
                @"pageSize":@(20),
                @"id":@(userID),
                @"pageNumber":@(self.VedioPageNum)
                };
    }else{
        self.VedioPageNum = 1;
        dic = @{@"token":SafeStr(token),
                @"pageSize":@(20),
                @"id":@(userID),
                @"pageNumber":@(self.VedioPageNum)
                };
    }
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest;
    if(isloadWorkVedio){
        quest = [HttpRequest buildRequest:@"/api/userWorks"];
    }else{
        quest = [HttpRequest buildRequest:@"/api/userLoves"];
    }
    [quest post:dic success:^(id data) {
        NSInteger total = [[data objectForKey:@"total"] integerValue];
        NSArray *list = [data objectForKey:@"list"];
        NSMutableArray *tempArr = [NSMutableArray array];
        NSError *error;
        if([list isKindOfClass:[NSArray class]] && list.count){
            tempArr = [RGRecommendVideoModel arrayOfModelsFromDictionaries:list error:&error];
        }
        if(isLoadMore){
            
            if([weakSelf.videoList count]){
                if(tempArr.count){
                    [weakSelf.videoList addObjectsFromArray:tempArr];
                }
            }else{
                weakSelf.videoList = tempArr;
            }
        }else{
            weakSelf.videoList = tempArr;
        }
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if([weakSelf.videoList count]){
                if(!isLoadMore){
                    if([weakSelf.protocol respondsToSelector:@selector(reloadTableViewWithVedioList:)]){
                        [weakSelf.protocol reloadTableViewWithVedioList:weakSelf.videoList];
                    }
                }else{
                    if([weakSelf.protocol respondsToSelector:@selector(reloadTableViewWhenHaveMoreVedioWithVedioList:)]){
                        [weakSelf.protocol reloadTableViewWhenHaveMoreVedioWithVedioList:weakSelf.videoList];
                    }
                }
                
            }else{
                if([weakSelf.protocol respondsToSelector:@selector(reloadTableViewWithVedioList:)]){
                    [weakSelf.protocol reloadTableViewWithVedioList:[NSMutableArray array]];
                }
            }
            
            if(isLoadMore){
                if([weakSelf.protocol respondsToSelector:@selector(viewMjFooterEndLoading)]){
                    [weakSelf.protocol viewMjFooterEndLoading];
                }
                
            }else{
                if([weakSelf.protocol respondsToSelector:@selector(viewMjHeaderEndLoading)]){
                    [weakSelf.protocol viewMjHeaderEndLoading];
                }
                if([weakSelf.protocol respondsToSelector:@selector(playWithFirstIndexWhenLoadSuccess)]){
                    [weakSelf.protocol playWithFirstIndexWhenLoadSuccess];
                }
            }
            
            if(list.count < PageSize){
                [weakSelf.protocol viewMjFooterStatusWithNoMoreData];
            }
            
        });
        
    } fail:^(NSInteger code, NSString *message) {
        
        if(isLoadMore){
            if([weakSelf.protocol respondsToSelector:@selector(viewMjFooterEndLoading)]){
                [weakSelf.protocol viewMjFooterEndLoading];
            }
            
        }else{
            if([weakSelf.protocol respondsToSelector:@selector(viewMjHeaderEndLoading)]){
                [weakSelf.protocol viewMjHeaderEndLoading];
            }
        }
    }];
}

@end
